package com.sense.kukoo.biz_library.view

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.sense.feel.common.utils.ToastUtils
import com.sense.kukoo.biz_library.R
import com.sense.kukoo.biz_library.adapter.MySubscriptionsAdapter
import com.sense.kukoo.biz_library.databinding.BizLibraryActivityMySubscriptionsBinding
import com.sense.kukoo.biz_library.vm.MySubscriptionsViewModel
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.common.mvvm.view.BaseMvvmActivity
import com.sense.kukoo.common.repo.bean.BookBean
import com.sense.kukoo.common.repo.bean.BookItemBean
import com.sense.kukoo.common.repo.bean.LibrarySubscriptionBean
import com.sense.kukoo.common.repo.bean.MySubscriptionsBean
import com.sense.kukoo.lib_logger.LoggerManager

@Route(path = ARouterUrl.ROUTE_LIBRARY_MODULE_MY_SUBSCRIPTIONS_ACTIVITY)
class MySubscriptionsActivity :
    BaseMvvmActivity<MySubscriptionsViewModel, BizLibraryActivityMySubscriptionsBinding>() {

    private val TAG: String = javaClass.simpleName
    private lateinit var mAdapter:MySubscriptionsAdapter

    override fun getLayoutId(): Int = R.layout.biz_library_activity_my_subscriptions

    override fun getViewModelClass(): Class<MySubscriptionsViewModel> =
        MySubscriptionsViewModel::class.java

    override fun responseVM(action: Int) {}

    override fun initData() {
        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
        //
        mBinding.titleView.init(this)
        setSupportActionBar(mBinding.titleView)
        //
        bindData(mViewModel.mMySubscriptions){
            mBinding.refreshLayout.finishRefresh()
            mBinding.refreshLayout.finishLoadMore()
            mAdapter.list = it
            mAdapter.notifyDataSetChanged()
        }
        bindData(mViewModel.enableLoadMore){
            mBinding.refreshLayout.setEnableLoadMore(it)
        }
        bindAction(mViewModel.mSubscripedChangedPosition){
            mAdapter.notifyItemChanged(it)
        }
    }

    override fun initView() {
        val mutableListOf = mutableListOf<MySubscriptionsBean>()
        mAdapter = MySubscriptionsAdapter(mListener, mutableListOf)
        mBinding.recyclerView.adapter = mAdapter
        //
        val linearLayoutManager = LinearLayoutManager(this)
        mBinding.recyclerView.layoutManager = linearLayoutManager
        mBinding.refreshLayout.setOnRefreshListener {
            //OnRefresh
            mViewModel.loadData()
        }
        mBinding.refreshLayout.setOnLoadMoreListener {
            //OnLoadMore
            mViewModel.getMoreData()
        }
    }

    override fun onResume() {
        super.onResume()
        mBinding.refreshLayout.autoRefresh()
    }

    val mListener = object : MySubscriptionsAdapter.MySubscriptionsItemEventListener {
        override fun onItemClick(bean: MySubscriptionsBean) {
            val book: LibrarySubscriptionBean = bean.item
            ARouter.getInstance()
                .build(ARouterUrl.ROUTE_MEDIA_MODULE_MEDIA_LIST_DETAIL_ACTIVITY)
                .withString(ARouterUrl.KEY_BOOK_ID_STR, ""+book.bookId)
                .navigation()
        }

        override fun onItemSubscripted(
            bean: MySubscriptionsBean,
            isSubscripted: Boolean,
            position: Int
        ) {
            mViewModel.setSubscripedChanged(bean, isSubscripted, position)
        }

    }
}